//
//  DCServiceTool.m
//  YoreachCarrier
//
//  Created by 大橙子 on 2018/6/7.
//  Copyright © 2018年 中都格罗唯视. All rights reserved.
//

#import "DCServiceTool.h"

@interface DCServiceTool ()
@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
@end
@implementation DCServiceTool

/**
 *  单例
 *
 *  @return 网络请求类的实例，可在请求时直接调用方法，也是一个直接初始化的方式
 */
+ (instancetype)defaultManager {
    static DCServiceTool *networkManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!networkManager) {
            networkManager = [[DCServiceTool alloc] init];
        }
    });
    return networkManager;
}

/**
 初始化，APP每次启动时会调用该方法，运行时不会调用
 @return 基本的请求设置
 */
- (instancetype)init {
    if (self = [super init]) {
        self.sessionManager = [AFHTTPSessionManager manager];
        // 设置请求以及相应的序列化器
        //        self.sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
        // 设置超时时间
        self.sessionManager.requestSerializer.timeoutInterval = 10.0;
        // 设置响应内容的类型
        //        self.sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
    }
    return self;
}

#pragma mark 常用网络请求
/**
 常用网络请求方式
 
 @param requestMethod 请求方试
 @param serverUrl 服务器地址
 @param parameters 参数
 @param progress 进度
 @param success 成功
 @param failure 失败
 @return return value description
 */
- (nullable NSURLSessionDataTask *)sendRequestMethod:(HTTPMethod)requestMethod
                                                 Url:(nonnull NSString *)serverUrl
                                          parameters:(nullable id)parameters
                                            progress:(nullable void (^)(NSProgress * _Nullable progress))progress
                                             success:(nullable void(^) (BOOL isSuccess, id _Nullable responseObject))success
                                             failure:(nullable void(^) (NSString * _Nullable errorMessage))failure {
    NSURLSessionDataTask * task = nil;
    switch (requestMethod) {
        case HTTPMethodGET:
        {
            task = [self.sessionManager GET:serverUrl parameters:parameters progress:progress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if (success) {
                    success(YES,responseObject);
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (failure) {
                    failure([self failHandleWithErrorResponse:error task:task]);
                }
            }];
        }
            break;
            
        case HTTPMethodPOST:
        {
            task = [self.sessionManager POST:serverUrl parameters:parameters progress:progress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if (success) {
                    success(YES,responseObject);
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (failure) {
                    failure([self failHandleWithErrorResponse:error task:task]);
                }
            }];
        }
            break;
            
        case HTTPMethodPUT:
        {
            task = [self.sessionManager PUT:serverUrl parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if (success) {
                    success(YES,responseObject);
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (failure) {
                    failure([self failHandleWithErrorResponse:error task:task]);
                }
            }];
        }
            break;
            
        case HTTPMethodPATCH:
        {
            task = [self.sessionManager PATCH:serverUrl parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if (success) {
                    success(YES,responseObject);
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (failure) {
                    failure([self failHandleWithErrorResponse:error task:task]);
                }
            }];
        }
            break;
            
        case HTTPMethodDELETE:
        {
            task = [self.sessionManager DELETE:serverUrl parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if (success) {
                    success(YES,responseObject);
                }            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    if (failure) {
                        failure([self failHandleWithErrorResponse:error task:task]);
                    }
                }];
        }
            break;
    }
    return task;
}


#pragma mark 报错信息
/**
 处理报错信息
 
 @param error AFN返回的错误信息
 @param task 任务
 @return description
 */
- (NSString *)failHandleWithErrorResponse:( NSError * _Nullable )error task:( NSURLSessionDataTask * _Nullable )task {
    __block NSString *message = nil;
    // 这里可以直接设定错误反馈，也可以利用AFN 的error信息直接解析展示
    NSData *afNetworking_errorMsg = [error.userInfo objectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];
    DCLog(@"afNetworking_errorMsg == %@",[[NSString alloc]initWithData:afNetworking_errorMsg encoding:NSUTF8StringEncoding]);
    if (!afNetworking_errorMsg) {
        message = @"网络连接失败";
    }
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
    NSInteger responseStatue = response.statusCode;
    if (responseStatue >= 500) {  // 网络错误
        message = @"服务器维护升级中,请耐心等待";
    } else if (responseStatue >= 400) {
        // 错误信息
        NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:afNetworking_errorMsg options:NSJSONReadingAllowFragments error:nil];
        message = responseObject[@"error"];
    }
    DCLog(@"error == %@",error);
    return message;
}

+(void)postWithUrl:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.securityPolicy = securityPolicy;
    if ([UserInfo shareInstance].token.length != 0) {
        [manager.requestSerializer setValue:[UserInfo shareInstance].token forHTTPHeaderField:@"token"];
    }
    
    [SVProgressHUD showWithStatus:@"数据加载中..."];
    NSMutableDictionary *auth = [NSMutableDictionary dictionary];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"auth"] = auth.mj_JSONString;
    dic[@"info"] = params.mj_JSONString;
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] integerValue] == 960001){//token失效

            [SVProgressHUD showWithStatus:@"用户token过期，请重新登录"];
            [SVProgressHUD dismissWithDelay:1.0 completion:^{

                [DCTool loginOut];
            }];
        }else
        {
            if (success) {
                success(responseObject);
            }
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (failure) {
            failure(error);
        }
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        [SVProgressHUD dismiss];
    }];
    
}
#pragma mark -- 上传图片
+ (void)uploadConstructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block success:(void (^)(NSString *imgStr))success failure:(void (^)(NSError * error))failure

{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:[UserInfo shareInstance].token forHTTPHeaderField:@"token"];
    [manager POST:DC_upload parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        block(formData);
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        CGFloat progress = 1.0f*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
        [SVProgressHUD showProgress:progress status:@"正在上传"];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 0) {
            !success?:success(responseObject[@"record"][@"filePath"]);
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"msg"]];
            [SVProgressHUD dismissWithDelay:1];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        !failure?:failure(error);
        
    }];
}
//质损上报多个图片
+ (void)uploadImg:(NSMutableArray *)imgArr success:(void(^)(NSArray *arr))success failure:(void(^)(NSError *error))failure
{
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.securityPolicy = securityPolicy;
    if ([UserInfo shareInstance].token.length != 0) {
        [manager.requestSerializer setValue:[UserInfo shareInstance].token forHTTPHeaderField:@"token"];
    }
    
    [manager POST:DC_multiupload parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        int i = 0;
        for (UIImage *image in imgArr) {
            NSData *data = UIImageJPEGRepresentation(image,0.5);
            
            NSString *fileName = [NSString stringWithFormat:@"file%d",i];
            //这个就是参数
            [formData appendPartWithFileData:data name:fileName fileName:@"*.jpg" mimeType:@"image/*"];
            i++;
        }

    } progress:^(NSProgress * _Nonnull uploadProgress) {
        CGFloat progress = 1.0f*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
        [SVProgressHUD showProgress:progress status:@"正在提交"];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DCLog(@"上传多张图片:%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 0) {
            //            [SVProgressHUD dismissWithDelay:1];
            
            NSMutableArray *imageArr = [NSMutableArray array];
            for (NSDictionary *file in responseObject[@"data"]) {
                NSString *filePath = file[@"filePath"];
                [imageArr addObject:filePath];
            }
            !success?:success(imageArr);
            //            [SVProgressHUD showWithStatus:@"提交成功!"];
            [SVProgressHUD dismissWithDelay:1];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"msg"]];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        !failure?:failure(error);
    }];
}

+ (void)uploadSingleImage:(UIImage *)image success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure {
    
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.securityPolicy = securityPolicy;
    if ([UserInfo shareInstance].token.length != 0) {
        [manager.requestSerializer setValue:[UserInfo shareInstance].token forHTTPHeaderField:@"token"];
    }
    
    [manager POST:DC_upload parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        
        formatter.dateFormat = @"yyyyMMddHHmmss";
        
        NSString *str = [formatter stringFromDate:[NSDate date]];
        
        //上面的这三句就是获取当前时间的，目的就是让上传的名字不同。
        NSData *data = UIImageJPEGRepresentation(image,0.5);
        
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        
        //这个就是参数
        [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/*"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        CGFloat progress = 1.0f*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
        [SVProgressHUD showProgress:progress status:@"正在提交"];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] integerValue] == 0) {
            !success?:success(responseObject[@"record"]);
            [SVProgressHUD dismissWithDelay:1];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"msg"]];
            [SVProgressHUD dismissWithDelay:1];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"上传图片失败"];
    }];
}
@end
